﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using com.byt.gestor.service;
using System.Data.Common;
using System.Data;
using com.byt.webcore;
using suce.upload;
using System.IO;
using System.Threading;
using System.Globalization;
//using Microsoft.Web.Services3.Security;
//using Microsoft.Web.Services3.Security.Tokens;
using com.byt.webcore.security;

namespace SUCE.SERVICE
{
    static class Program
    {
        public static Transmitir trans;

        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static void Main(string[] args)
        {

            try
            {
                Thread.CurrentThread.CurrentCulture = new CultureInfo("es-PE"); 

                if (args.Length > 0 && "config".Equals(args[0].ToLower()))
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Config());
                }
                else if (args.Length > 0 && "auto".Equals(args[0].ToLower()))
                {
                    Application.EnableVisualStyles();
                    Application.SetCompatibleTextRenderingDefault(false);
                    Application.Run(new Service());
                }
                else
                {
                    Sincronizar();
                }
            }
            catch (Exception e)
            {
                Log(e);
            }

        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            //MessageBox.Show(e.Exception.Message);
            string fn = Path.GetDirectoryName(Application.ExecutablePath);
            fn = Path.Combine(fn, "Log.log");
            File.AppendAllText(fn, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " --> " + e.Exception.Message + Environment.NewLine);
        }

        internal static void Warning(string e)
        {
            //MessageBox.Show(e.Exception.Message);
            string fn = Path.GetDirectoryName(Application.ExecutablePath);
            fn = Path.Combine(fn, "Log.log");
            File.AppendAllText(fn, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " --> " + e + Environment.NewLine + Environment.NewLine);
        }

        internal static void Warning(Exception e)
        {
            Warning(e.Message);
        }

        internal static void Log(Exception e)
        {
            //MessageBox.Show(e.Exception.Message);
            string fn = Path.GetDirectoryName(Application.ExecutablePath);
            fn = Path.Combine(fn, "Log.log");
            File.AppendAllText(fn, DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + " --> " + e.Message + Environment.NewLine);
            Application.Exit();
        }

        static public string GetPassword(string toEncode)
        {
            byte[] toEncodeAsBytes = System.Text.ASCIIEncoding.ASCII.GetBytes(toEncode);
            string returnValue = System.Convert.ToBase64String(toEncodeAsBytes);
            return returnValue;
        }

        internal static void Sincronizar()
        {
            Transmitir trans = new Transmitir();
            Recepcionar recep = new Recepcionar();
        }
    }
}